package com.jiejie.controller;

import com.jiejie.entity.Movie;
import com.jiejie.service.EsMovieService;
import com.jiejie.service.EsMovieService2;
import com.jiejie.service.IMovieService;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
public class EsController {
    @Resource
    private ElasticsearchRestTemplate restTemplate;
    @Resource
    private EsMovieService esMovieService;
    @Resource
    private IMovieService movieService;
    @Resource
    private EsMovieService2 esMovieService2;

    @GetMapping("saveMovie")
    public String saveMovie(){
        List<Movie> movieList=movieService.getAllMovie();
        Iterable<Movie> movieIterable = esMovieService2.saveAll(movieList);
        return "save successfully";
    }


    @GetMapping("query")
    public ModelAndView getHighByPage(@RequestParam("movieName")String movieName,
                                      @RequestParam("pageNum")Integer pageNum){
        List<Movie> list=esMovieService.getMovieList(movieName,pageNum);
        System.out.println("list"+list);
        ModelAndView mav=new ModelAndView();
        mav.addObject("list",list);
        mav.addObject("movieName",movieName);

        mav.setViewName("pages/query");
        System.out.println(movieName);
        return mav;
    }
    @GetMapping("querySearch")
    public List<Movie> getMovieByPage(@RequestParam("movieName") String movieName,
                                            @RequestParam("pageNum")Integer pageNum){

        return esMovieService.getMovieList(movieName,pageNum);
    }
}
